package com.turing.data.scripts.contract.filter


import com.turing.common.util.FileUtils
import com.turing.common.util.Tools
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO

class AfterIfAvailable_30182_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_BACKUP = '备案完成前'
        def REGEX_BACKUP_2 = '现金管理'

        List< TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        def set = new HashSet()

        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
//                    return  resultVOs
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 150 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,start)
                    if( expandString =~ REGEX_BACKUP ){
                        if(!set.contains(drawResultVO.text)){
                            trueValueSet.add(drawResultVO)
                            set.add(drawResultVO.text)
                        }

                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 20 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,start)
                    if( expandString =~ REGEX_BACKUP_2 ){
                        if(!set.contains(drawResultVO.text)){
                            trueValueSet.add(drawResultVO)
                            set.add(drawResultVO.text)
                        }

                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = ""
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet

    }

    public static void main(String[] args) {

        String path = "D:\\workspace\\Extractor\\data\\src\\main\\java\\com\\turing\\data\\scripts\\contract\\filter";
        File file = new File(path);
        if(file.isDirectory()){
            List<File> files = file.listFiles()
            for(File file0: files){
                String newFileName = file0.getAbsolutePath().replace("_10_","_10_")
                file0.renameTo(new File(newFileName))
            }
        }

    }

}
